//
//  USSendStatusButton.m
//  us
//
//  Created by karry on 2025/4/16.
//

#import "USSendStatusView.h"

@interface USSendStatusView ()

@property (nonatomic, strong) UIActivityIndicatorView *indicator;
@property (nonatomic, strong) UIButton *retryButton;

@end

@implementation USSendStatusView

- (instancetype)init {
    if (self = [super initWithFrame:CGRectMake(0, 0, 30, 30)]) {
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    self.indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleMedium];
    self.indicator.hidesWhenStopped = YES;
    [self addSubview:self.indicator];

    self.retryButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.retryButton setImage:[UIImage imageNamed:@"chat_error"] forState:UIControlStateNormal];
    [self.retryButton addTarget:self action:@selector(retryTapped) forControlEvents:UIControlEventTouchUpInside];
    self.retryButton.hidden = YES;
    [self addSubview:self.retryButton];

    // 使用 Masonry 布局
    [self.indicator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.height.mas_equalTo(20);
    }];
    [self.retryButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.height.mas_equalTo(24);
    }];
}

- (void)setStatus:(EMMessageStatus)status{
    _status = status;
 
    switch (status) {
        case EMMessageStatusPending:
            [self.indicator stopAnimating];
            self.retryButton.hidden = YES;
            self.hidden = NO;
            break;
        case EMMessageStatusDelivering:
            [self.indicator startAnimating];
            self.retryButton.hidden = YES;
            self.hidden = NO;
            break;
        case EMMessageStatusSucceed:
            [self.indicator stopAnimating];
            self.retryButton.hidden = YES;
            self.hidden = YES;
            break;
        case EMMessageStatusFailed:
            [self.indicator stopAnimating];
            self.retryButton.hidden = NO;
            self.hidden = NO;
            break;
    }
}

- (void)retryTapped {
    if ([self.delegate respondsToSelector:@selector(sendStatusViewDidTapRetry:)]) {
        [self.delegate sendStatusViewDidTapRetry:self];
    }
}

@end
